/*
 * Copyright 2000-2020 ALPHA LAB.Inc All Rights Reserved.
 */
package com.alphalab.framework.context;

import java.io.Serializable; 
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alphalab.framework.domain.AbstractValueObject;
import com.alphalab.framework.domain.dto.ResultVO;
import com.alphalab.framework.domain.dto.SingleResultVO;
import com.alphalab.framework.log.IMoLog;
import com.alphalab.framework.log.MoLogFactory;
import com.alphalab.framework.role.IGeneralRole;

/**
 * 应对各种场景下的业务处理，属于服务交互层.
 * 主要处理增删改及基本查询.
 * @param <VO>
 * @param <ROLE>
 * @author allen 
 * @version 1.0.0 2017年8月15日
 */
public class GeneralContext <VO extends AbstractValueObject, ROLE extends IGeneralRole<VO>> implements 
	IGeneralContext<VO> {
	
	/**
	 * logger.
	 */
	private static final IMoLog logger = MoLogFactory.getLog(GeneralContext.class);

	/**
	 * generalRole.
	 */
	private ROLE generalRole; 
	
	/**
	 * 获取generalRole.
	 * @return ROLE
	 */
	public ROLE getGeneralRole() {
		return generalRole;
	}

	/**
	 * 设置generalRole.
	 * @param newgeneralRole the generalRole to set.
	 */
	public void setGeneralRole(final ROLE newgeneralRole) {
		this.generalRole = newgeneralRole;
	}

	/**
	 * 保存VO对象.
	 * @param valueObject VO对象
	 * @return ResultVO
	 */
	@RequestMapping("/save")
	@ResponseBody
	@Override
	public ResultVO save(final VO valueObject) {
		ResultVO rstVo = new ResultVO();
		try {
			getGeneralRole().save(valueObject);
			rstVo.setSuccessResult();
		} catch (Exception ex) {
			logger.error(ex);
			rstVo = new ResultVO(ex);
		}
		return rstVo;
	}

	/**
	 * 保存VO对象集合.
	 * @param valueObjects VO对象集合
	 * @return ResultVO
	 */
	@RequestMapping("/saveList")
	@ResponseBody
	@Override
	public ResultVO saveList(final List<VO> valueObjects) {
		ResultVO rstVo = new ResultVO();
		try {
			getGeneralRole().saveList(valueObjects);
			rstVo.setSuccessResult();
		} catch (Exception ex) {
			logger.error(ex);
			rstVo = new ResultVO(ex);
		}
		return rstVo;
	}

	/**
	 * 删除VO对象.
	 * @param valueObject VO对象
	 * @return ResultVO
	 */
	@RequestMapping("/delete")
	@ResponseBody
	@Override
	public ResultVO delete(final VO valueObject) {
		ResultVO rstVo = new ResultVO();
		try {
			getGeneralRole().delete(valueObject);
			rstVo.setSuccessResult();
		} catch (Exception ex) {
			logger.error(ex);
			rstVo = new ResultVO(ex);
		}
		return rstVo;
	}

	/**
	 * 删除VO对象.
	 * @param id 主键
	 * @return ResultVO
	 */
	@RequestMapping("/deleteById")
	@ResponseBody
	@Override
	public ResultVO deleteById(final Serializable id) {
		ResultVO rstVo = new ResultVO();
		try {
			getGeneralRole().deleteById(id);
			rstVo.setSuccessResult();
		} catch (Exception ex) {
			logger.error(ex);
			rstVo = new ResultVO(ex);
		}
		return rstVo;
	}

	/**
	 * 删除VO对象集合.
	 * @param valueObjects VO对象集合
	 * @return ResultVO
	 */
	@RequestMapping("/deleteList")
	@ResponseBody
	@Override
	public ResultVO deleteList(final List<VO> valueObjects) {
		ResultVO rstVo = new ResultVO();
		try {
			getGeneralRole().deleteList(valueObjects);
			rstVo.setSuccessResult();
		} catch (Exception ex) {
			logger.error(ex);
			rstVo = new ResultVO(ex);
		}
		return rstVo;
	}

	/**
	 * 更新VO对象.
	 * @param valueObject VO对象
	 * @return ResultVO
	 */
	@RequestMapping("/update")
	@ResponseBody
	@Override
	public ResultVO update(final VO valueObject) {
		ResultVO rstVo = new ResultVO();
		try {
			getGeneralRole().update(valueObject);
			rstVo.setSuccessResult();
		} catch (Exception ex) {
			logger.error(ex);
			rstVo = new ResultVO(ex);
		}
		return rstVo;
	}

	/**
	 * 更新VO对象集合.
	 * @param valueObjects VO对象集合
	 * @return ResultVO
	 */
	@RequestMapping("/updateList")
	@ResponseBody
	@Override
	public ResultVO updateList(final List<VO> valueObjects) {
		ResultVO rstVo = new ResultVO();
		try {
			getGeneralRole().updateList(valueObjects);
			rstVo.setSuccessResult();
		} catch (Exception ex) {
			logger.error(ex);
			rstVo = new ResultVO(ex);
		}
		return rstVo;
	}

	/**
	 * 保存或更新VO对象.
	 * @param valueObject VO对象
	 * @return ResultVO
	 */
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	@Override
	public ResultVO saveOrUpdate(final VO valueObject) {
		ResultVO rstVo = new ResultVO();
		try {
			getGeneralRole().saveOrUpdate(valueObject);
			rstVo.setSuccessResult();
		} catch (Exception ex) {
			logger.error(ex);
			rstVo = new ResultVO(ex);
		}
		return rstVo;
	}

	/**
	 * 保存或更新VO对象集合.
	 * @param valueObjects VO对象集合
	 * @return ResultVO
	 */
	@RequestMapping("/saveOrUpdateList")
	@ResponseBody
	@Override
	public ResultVO saveOrUpdateList(final List<VO> valueObjects) {
		ResultVO rstVo = new ResultVO();
		try {
			getGeneralRole().saveOrUpdateList(valueObjects);
			rstVo.setSuccessResult();
		} catch (Exception ex) {
			logger.error(ex);
			rstVo = new ResultVO(ex);
		}
		return rstVo;
	}

	/**
	 * 根据主键返回对象.
	 * @param id 主键
	 * @return VO
	 */
	@RequestMapping("/find")
	@ResponseBody
	@Override
	public SingleResultVO<VO> find(final Serializable id) {
		VO vo = null;
		SingleResultVO<VO> rstVo = new SingleResultVO<VO>();
		try {
			vo = getGeneralRole().find(id);
			rstVo = new SingleResultVO(vo);
		} catch (Exception ex) {
			logger.error(ex);
			rstVo = new SingleResultVO(ex); 
		}
		return rstVo;
	} 

}
